using MicroMsg.Common.Utils;
using System;
using System.Device.Location;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Threading;
namespace MicroMsg.Source.UI.Chat.Map
{
	public class OffsetConverter
	{
		public delegate void OnLoadDataCompeleted();
		private const string TAG = "OffsetConverter";
		private double[] TableX = new double[297000];
		private double[] TableY = new double[297000];
		private bool? _isInChina = default(bool?);
		private HttpWebRequest _request;
		private DispatcherTimer _dtTimeOut;
		private bool _isTimeOut;
		public bool HasLoadData;
		private bool IsLoadingData;
		public event EventHandler OnGetNoOffsetLocationCompeleted;
		public void Dispose()
		{
			this.TableX = null;
			this.TableY = null;
		}
		public void GetOffsetLocation(GeoCoordinate location)
		{
			this.CheckIsInChina(location);
		}
		public void LoadData()
		{
			if (!this.HasLoadData && !this.IsLoadingData)
			{
				this.IsLoadingData = true;
				Log.smethod_5("OffsetConverter", "Start Load OffsetData");
				try
				{
					using (Stream stream = Application.GetResourceStream(new Uri("Resource/Map/OffsetData", 2)).get_Stream())
					{
						int num = 0;
						while (true)
						{
							byte[] array = new byte[4];
							if (stream.Read(array, 0, 4) != 4)
							{
								break;
							}
							if (num < 297000)
							{
								this.TableX[num] = (double)BitConverter.ToInt32(array, 0) / 100000.0;
							}
							else
							{
								this.TableY[num - 297000] = (double)BitConverter.ToInt32(array, 0) / 100000.0;
							}
							num++;
						}
					}
					this.HasLoadData = true;
				}
				catch
				{
					Log.smethod_5("OffsetConverter", "Load OffsetData Error");
				}
				Log.smethod_5("OffsetConverter", "End Load OffsetData");
				this.IsLoadingData = false;
			}
		}
		private void CheckIsInChina(GeoCoordinate location)
		{
			Log.smethod_5("OffsetConverter", "Start CheckIsInChina");
			if (this._request != null && !this._request.get_HaveResponse())
			{
				this._request.Abort();
			}
			this._isTimeOut = false;
			this._request = (HttpWebRequest)WebRequest.Create(string.Concat(new string[]
			{
				"http://maps.google.com/maps/api/geocode/xml?latlng=",
				location.get_Latitude().ToString(),
				",",
				location.get_Longitude().ToString(),
				"&sensor=false&language=zh-cn"
			}));
			this._request.BeginGetResponse(new AsyncCallback(this.GeocodeXmlCallback), location);
			this._dtTimeOut = new DispatcherTimer();
			this._dtTimeOut.set_Interval(new TimeSpan(0, 0, 5));
			this._dtTimeOut.add_Tick(new EventHandler(this.TimeOut_Tick));
			this._dtTimeOut.Start();
		}
		private void TimeOut_Tick(object sender, EventArgs e)
		{
			if (!this._request.get_HaveResponse())
			{
				this._isTimeOut = true;
				this._request.Abort();
				this._dtTimeOut.Stop();
				this._dtTimeOut.remove_Tick(new EventHandler(this.TimeOut_Tick));
			}
		}
		private void GeocodeXmlCallback(IAsyncResult asyncResult)
		{
			try
			{
				if (!this._isTimeOut)
				{
					HttpWebResponse httpWebResponse = (HttpWebResponse)this._request.EndGetResponse(asyncResult);
					using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
					{
						string text = streamReader.ReadToEnd();
						if (!string.IsNullOrEmpty(text))
						{
							if (!text.Contains("<short_name>CN</short_name>") && !text.Contains("<long_name>中国</long_name>"))
							{
								this._isInChina = new bool?(false);
							}
							else
							{
								this._isInChina = new bool?(true);
							}
							GeoCoordinate location = (GeoCoordinate)asyncResult.get_AsyncState();
							Log.smethod_5("OffsetConverter", "End CheckIsInChina Successful:" + this._isInChina);
							this.ConverLocation(location);
						}
						else
						{
							if (this._isTimeOut)
							{
								Log.smethod_5("OffsetConverter", "CheckIsInChina TimeOut");
								this.ConverLocation((GeoCoordinate)asyncResult.get_AsyncState());
							}
						}
						goto IL_E5;
					}
				}
				this.ConverLocation((GeoCoordinate)asyncResult.get_AsyncState());
				IL_E5:;
			}
			catch
			{
				this.ConverLocation((GeoCoordinate)asyncResult.get_AsyncState());
			}
		}
		private int GetID(int I, int J)
		{
			return I + 660 * J;
		}
		private void ConverLocation(GeoCoordinate location)
		{
			try
			{
				Log.smethod_5("OffsetConverter", "Start ConverLocation");
				double latitude = location.get_Latitude();
				double longitude = location.get_Longitude();
				double num = longitude;
				double num2 = latitude;
				for (int i = 0; i < 10; i++)
				{
					if (!this._isInChina.get_HasValue())
					{
						if (num >= 72.0 && num <= 137.9 && num2 >= 10.0)
						{
							if (num2 <= 54.9)
							{
								goto IL_94;
							}
						}
						this.OnGetNoOffsetLocationCompeleted.Invoke(location, null);
						return;
					}
					bool? isInChina = this._isInChina;
					if (!isInChina.GetValueOrDefault() && isInChina.get_HasValue())
					{
						this.OnGetNoOffsetLocationCompeleted.Invoke(location, null);
						return;
					}
					IL_94:
					int num3 = (int)((num - 72.0) * 10.0);
					int num4 = (int)((num2 - 10.0) * 10.0);
					double num5 = this.TableX[this.GetID(num3, num4)];
					double num6 = this.TableY[this.GetID(num3, num4)];
					double num7 = this.TableX[this.GetID(num3 + 1, num4)];
					double num8 = this.TableY[this.GetID(num3 + 1, num4)];
					double num9 = this.TableX[this.GetID(num3 + 1, num4 + 1)];
					double num10 = this.TableY[this.GetID(num3 + 1, num4 + 1)];
					double num11 = this.TableX[this.GetID(num3, num4 + 1)];
					double num12 = this.TableY[this.GetID(num3, num4 + 1)];
					double num13 = (num - 72.0 - 0.1 * (double)num3) * 10.0;
					double num14 = (num2 - 10.0 - 0.1 * (double)num4) * 10.0;
					double num15 = num - ((1.0 - num13) * (1.0 - num14) * num5 + num13 * (1.0 - num14) * num7 + num13 * num14 * num9 + (1.0 - num13) * num14 * num11);
					double num16 = num2 - ((1.0 - num13) * (1.0 - num14) * num6 + num13 * (1.0 - num14) * num8 + num13 * num14 * num10 + (1.0 - num13) * num14 * num12);
					num = (num + longitude - num15) / 2.0;
					num2 = (num2 + latitude - num16) / 2.0;
				}
				double longitude2 = num;
				double latitude2 = num2;
				GeoCoordinate geoCoordinate = new GeoCoordinate();
				geoCoordinate.set_Latitude(latitude2);
				geoCoordinate.set_Longitude(longitude2);
				geoCoordinate.set_Altitude(location.get_Altitude());
				geoCoordinate.set_Course(location.get_Course());
				geoCoordinate.set_HorizontalAccuracy(location.get_HorizontalAccuracy());
				geoCoordinate.set_VerticalAccuracy(location.get_VerticalAccuracy());
				geoCoordinate.set_Speed(location.get_Speed());
				GeoCoordinate geoCoordinate2 = geoCoordinate;
				this.OnGetNoOffsetLocationCompeleted.Invoke(geoCoordinate2, null);
			}
			catch
			{
				this.OnGetNoOffsetLocationCompeleted.Invoke(location, null);
			}
		}
	}
}
